package com.apache.passport.common;

import com.apache.cache.service.impl.redis.JedisSsoUtil;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.passport.entity.LonErrToken;
import com.apache.passport.entity.Token;
import com.apache.passport.service.CacheHelper;

/**
 * description:  常量类
 *
 * @author Hou Dayu 创建时间：2016-12-18
 */
public class PassPortConst {

    private PassPortConst() {
        ///
    }

    /**
     * 过滤器不拦截的扩展名
     */
    public static final String SUFFIX = "js,css,png,jpg,gif,bmp,swf,fla,ico";

    private static final String TOKEN_ERROR_KEY = "loginErrToken";

    private static final String TOKEN_KEY = "loginToken";

    private static final String TOKEN_USERNAME_KEY = "userEnaemForTokenId";

    /**
     * description:  获取登录次数限制缓存对象
     *
     * @param remoteIp 请求IP地址
     * @param userEname 登录用户名
     * @return
     */
    public static LonErrToken getLonErrToken(String remoteIp, String userEname) {
        LonErrToken token = null;
        if (JedisSsoUtil.getInstance().isRedisCache()) {
            token = (LonErrToken) JedisSsoUtil.getInstance()
                    .getCacheObjectByKey("loginErrToken_" + remoteIp + userEname);
        } else {
            token = (LonErrToken) CacheHelper.getInstance().getCache(TOKEN_ERROR_KEY)
                    .getCacheCloneByKey(remoteIp + userEname);
        }
        return token;
    }

    public static void setLonErrToken(String remoteIp, String userEname, LonErrToken token) {
        if (JedisSsoUtil.getInstance().isRedisCache()) {
            JedisSsoUtil.getInstance().setLoginErrToken(remoteIp + userEname, 0, token);
        } else {
            CacheHelper.getInstance().getCache(TOKEN_ERROR_KEY)
                    .createCacheObject(remoteIp + userEname, token);
        }
    }

    public static void detLonErrToken(String remoteIp, String userEname) {
        if (JedisSsoUtil.getInstance().isRedisCache()) {
            JedisSsoUtil.getInstance().removeCacheObject("loginErrToken_" + remoteIp + userEname);
            JedisSsoUtil.getInstance().delListInfo("loginErrToken","loginErrToken_" + remoteIp + userEname);
        } else {
            CacheHelper.getInstance().getCache(TOKEN_ERROR_KEY)
                    .removeCacheObject(remoteIp + userEname);
        }
    }

    /**
     * description:  获取登录缓存对象
     *
     * @param tokenId
     * @return
     */
    public static Token getToken(String tokenId) {
        Token token = null;
        if (JedisSsoUtil.getInstance().isRedisCache()) {
            token = (Token) JedisSsoUtil.getInstance().getCacheObjectByKey("loginToken_" + tokenId);
        } else {
            token = (Token) CacheHelper.getInstance().getCache(TOKEN_KEY)
                    .getCacheCloneByKey(tokenId);
        }
        return token;
    }

    public static void setToken(boolean synFlag, String tokenId, Token token) {
        if (JedisSsoUtil.getInstance().isRedisCache()) {
            if (synFlag) {
                String tokenMapId = JedisSsoUtil.getInstance()
                        .getUserEnaemForTokenId(token.getUserEname());
                if (!Validator.isNull(tokenMapId)) {
                    SystemTools.getInstance().getCache("")
                            .removeCacheObject("loginToken_" + tokenMapId);
                }
                JedisSsoUtil.getInstance().setUserEnaemForTokenId(token.getUserEname(), tokenId, 0);
            }
            //存储缓存
            JedisSsoUtil.getInstance().setLoginToken(tokenId, 0, token);
        } else {
            if (synFlag) {
                String tokenMapId = (String) CacheHelper.getInstance().getCache(TOKEN_USERNAME_KEY)
                        .getCacheCloneByKey(token.getUserEname());
                if (!Validator.isNull(tokenMapId)) {
                    CacheHelper.getInstance().getCache(TOKEN_KEY).removeCacheObject(tokenMapId);
                }
                CacheHelper.getInstance().getCache(TOKEN_USERNAME_KEY)
                        .createCacheObject(token.getUserEname(), tokenId);
            }
            //存储缓存
            CacheHelper.getInstance().getCache(TOKEN_KEY).createCacheObject(tokenId, token);
        }
    }

    public static void detToken(String tokenId) {
        if (JedisSsoUtil.getInstance().isRedisCache()) {
            JedisSsoUtil.getInstance().removeCacheObject("loginToken_" + tokenId);
            JedisSsoUtil.getInstance().delListInfo("loginToken","loginToken_" + tokenId);
        } else {
            CacheHelper.getInstance().getCache(TOKEN_KEY).removeCacheObject(tokenId);
        }
    }

    /**
     * description:  如果属于单态,使用单态只应用在登录场景,如果在飞单态情况下登录成功
     *
     * @param desEname 加密的用户名
     * @return
     */
    public static String getUserEnaemForTokenId(String desEname) {
        if (JedisSsoUtil.getInstance().isRedisCache()) {
            return String.valueOf(JedisSsoUtil.getInstance().getUserEnaemForTokenId(desEname));
        } else {
            return String.valueOf(CacheHelper.getInstance().getCache(TOKEN_USERNAME_KEY)
                    .getCacheCloneByKey(desEname));
        }
    }

}
